package com.polaris.live.adapter

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.databinding.AdapterContributionItemBinding
import com.polaris.live.resp.back_resp.Rank
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * ContributionItemAdapter 貢獻幫adapter
 * @author Created by 天晴 on 2024/5/28 11:30
 * @since 1.0.0
 **/
class ContributionItemAdapter : BaseAdapter<Rank, ContributionItemAdapter.GamePlayWinHolder>(0) {

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): GamePlayWinHolder {
        return GamePlayWinHolder(
            AdapterContributionItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun convert(holder: GamePlayWinHolder, item: Rank) {
        holder.binding.levelTv.text = "${holder.absoluteAdapterPosition + 4}"
        holder.binding.name.text = item.user.nickname
        holder.binding.money.text = item.score.formatNumberWithK()
        holder.binding.acFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(item.user.avatarFrame))
        Glide.with(context)
            .load(ImageUtils.imgPathSmall(item.user.avatar))
            .transform(CircleCrop())
            .into(holder.binding.avater)
        if (TextUtils.isEmpty(item.user.country)) {
            holder.binding.userCountry.visibility = View.GONE
        } else {
            holder.binding.userCountry.visibility = View.VISIBLE
            holder.binding.userCountry.setCountry(item.user.country)
        }
        holder.binding.vipView.setVipLevel(item.user.vipLevel)
        holder.binding.levelView.setRichCharmBySexLevel(
            item.user.sex,
            item.user.richs,
            item.user.charms
        )

        when (val state = UserStateUtils.resolveUserState(item.user)) {
            UserStateUtils.USER_STATE_ONLINE, UserStateUtils.USER_STATE_OFFLINE, UserStateUtils.USER_STATE_BUSY -> {
                holder.binding.vState.visibility = View.VISIBLE
                holder.binding.ivStateLive.visibility = View.GONE
                when (state) {
                    UserStateUtils.USER_STATE_ONLINE -> {
                        holder.binding.vState.setBackgroundResource(R.drawable.ic_user_space)
                    }

                    UserStateUtils.USER_STATE_OFFLINE -> {
                        holder.binding.vState.setBackgroundResource(R.drawable.shape_session_state_offline)
                    }

                    UserStateUtils.USER_STATE_BUSY -> {
                        holder.binding.vState.setBackgroundResource(R.drawable.ic_user_space)
//                        holder.binding.vState.setBackgroundResource(R.drawable.shape_session_state_busy)
                    }

                    else -> {}
                }
            }

            UserStateUtils.USER_STATE_LIVE, UserStateUtils.USER_STATE_MATCH, UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_GAME -> {
                holder.binding.vState.visibility = View.GONE
                holder.binding.ivStateLive.visibility = View.VISIBLE
                when (state) {
                    UserStateUtils.USER_STATE_LIVE -> {
                        holder.binding.vState.visibility = View.VISIBLE
                        holder.binding.ivStateLive.visibility = View.GONE
                        holder.binding.vState.setBackgroundResource(R.drawable.ic_user_space)
//                        holder.binding.ivStateLive.setImageResource(null)
                    }

                    UserStateUtils.USER_STATE_MATCH -> {
                        holder.binding.vState.visibility = View.VISIBLE
                        holder.binding.ivStateLive.visibility = View.GONE
                        holder.binding.vState.setBackgroundResource(R.drawable.ic_user_space)
//                        holder.binding.ivStateLive.setImageResource(R.drawable.icon_list_match)
                        holder.binding.ivStateLive.setImageResource(null)
                    }

                    UserStateUtils.USER_STATE_PARTY -> {
                        holder.binding.ivStateLive.setImageResource(R.drawable.icon_party)
                    }

                    UserStateUtils.USER_STATE_GAME -> {
                        holder.binding.ivStateLive.setImageResource(R.drawable.icon_list_gameing)
                    }

                    else -> {}
                }
            }

            else -> {
                holder.binding.vState.visibility = View.GONE
                holder.binding.ivStateLive.visibility = View.GONE
            }
        }
    }

    inner class GamePlayWinHolder(val binding: AdapterContributionItemBinding) :
        BaseViewHolder(binding.root)
}